using System;
using System.Collections.Generic;
using System.Text;



using CreamX.Framework.Game.Components;
using CreamX.Framework.Game;
using CreamX.Framework.Attributes;

namespace CreamXComponentPack
{
    public delegate void OnKilledHandler(CreamXObject ourObject,CreamXObject killedBy);
    [CreamXComponent("HealthComponent")]
    public class HealthComponent:CreamXComponent
    {
        public static OnKilledHandler OnKilled = new OnKilledHandler(ThisOnKilled);
        public static void ThisOnKilled(CreamXObject ourObject, CreamXObject killedBy)
        {
        }
        private float _health;
        public float _maxHealth;
        public float _minHealth;
        private bool _bleedHealth;
        private float _bleedRate;

        public HealthComponent()
        {
        }
        public override void OnRegister()
        {
            Enabled = true;
            base.OnRegister();
        }
        [CreamXComponentProperty("Health")]
        public float Health
        {
            get { return _health; }
            set { _health = value; }
        }
        
        [CreamXComponentProperty("MinHealth")]
        public float MinHealth
        {
            get { return _minHealth; }
            set { _minHealth = value; }
        }
        
        [CreamXComponentProperty("MaxHealth")]
        public float MaxHealth
        {
            get { return _maxHealth; }
            set { _maxHealth = value; }
        }

        [CreamXComponentProperty("BleedHealth")]
        public bool BleedHealth
        {
            get { return _bleedHealth; }
            set { _bleedHealth = value; }
        }

        [CreamXComponentProperty("BleedRate")]
        public float BleedRate
        {
            get { return _bleedRate; }
            set { _bleedRate = value; }
        }
        public CreamXObject lastHitBy;
        public override void Update(float elapsed)
        {
            if (BleedHealth)
            {
                Health -= BleedRate*elapsed;
            }
            if (Health <= MinHealth)
            {
                SceneObject.MarkForDelete = true;
                OnKilled(this.SceneObject, lastHitBy);
            }
            base.Update(elapsed);
        }
        public void Hit(float amount,CreamXObject obj)
        {
            lastHitBy = obj;
            bool dodamage = true;
            if (OnHit != null)
                OnHit(obj, ref dodamage);
            if(dodamage)
                Health -= amount;
        }
        public bool IsDead
        {
            get { return _health <= _minHealth; }
        }
        public OnHitHandler OnHit;
    }
    public delegate void OnHitHandler(CreamXObject obj,ref bool dodamage);
}
